package com.kun.valiant.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kun.valiant.channel.domain.Channel;
import com.kun.valiant.channel.domain.vo.ChannelVO;
import com.kun.valiant.channel.mapper.ChannelMapper;
import com.kun.valiant.channel.service.IChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.domain.model.PieData;
import com.kun.valiant.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fangkun
 * @since 2023-06-01
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {

    @Autowired
    ChannelMapper channelMapper;

    @Override
    public List<Channel> selectChannelList(ChannelVO channelVO) {
        return channelMapper.selectChannelList(channelVO);
    }

    @Override
    public AjaxResult addChannel(ChannelVO channelVO) {
        //不能添加已经存在的渠道
        LambdaQueryWrapper<Channel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Channel::getChannelName,channelVO.getChannelName()).eq(Channel::getDelFlag,"0");
        Channel c = getOne(wrapper);

        if (c != null){
            return AjaxResult.error("添加失败，不能重复添加渠道！");
        }
        //添加
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelVO,channel);
        channel.setCreateBy(SecurityUtils.getUsername());
        channel.setCreateTime(LocalDateTime.now());
        channel.setDelFlag(0);
        return save(channel) ? AjaxResult.success("添加成功!") : AjaxResult.error("添加失败！");
    }

    @Override
    public AjaxResult editChannel(ChannelVO channelVO) {
        //不能添加已经存在的渠道
        LambdaQueryWrapper<Channel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Channel::getChannelName,channelVO.getChannelName())
                .eq(Channel::getDelFlag,"0")
                .ne(Channel::getChannelId,channelVO.getChannelId());
        Channel c = getOne(wrapper);

        if (c != null){
            return AjaxResult.error("修改失败，已经存在相同的渠道！");
        }

        Channel channel = new Channel();
        BeanUtils.copyProperties(channelVO,channel);
        channel.setUpdateBy(SecurityUtils.getUsername());
        channel.setUpdateTime(LocalDateTime.now());
        return updateById(channel) ? AjaxResult.success("修改成功!") : AjaxResult.error("修改失败！");
    }

    @Override
    public boolean removeChannelIds(Long[] channelIds) {
        LambdaUpdateWrapper<Channel> uw = new LambdaUpdateWrapper<>();
        uw.set(Channel::getDelFlag,1).in(Channel::getChannelId,channelIds);
        return update(uw);
    }

    @Override
    public boolean importChannel(List<Channel> channelList, boolean updateSupport) {
        if (updateSupport){
            //根据id更新
            List<Channel> channels = channelList.stream().map(c -> {
                c.setUpdateBy(SecurityUtils.getUsername());
                c.setUpdateTime(LocalDateTime.now());
                return c;
            }).collect(Collectors.toList());
            return updateBatchById(channels);
        }else{
            //插入
            List<Channel> channels = channelList.stream().map(c -> {
                c.setCreateBy(SecurityUtils.getUsername());
                c.setCreateTime(LocalDateTime.now());
                c.setDelFlag(0);
                c.setChannelId(null);
                return c;
            }).collect(Collectors.toList());
            return saveBatch(channels);
        }
    }

    @Override
    public AjaxResult channelAnalysisData(ChannelVO channelVO) {
        if (channelVO.getParams().get("beginTime")==null||channelVO.getParams().get("endTime")==null) {
            channelVO.getParams().put("beginTime", LocalDateTime.now().minusWeeks(1));
            channelVO.getParams().put("endTime", LocalDateTime.now().plusWeeks(1));
        }
        List<PieData> list = channelMapper.channelAnalysisData(channelVO);
        return AjaxResult.success(list);
    }

}
